import time
import pymysql


def get_time():
    time_str = time.strftime("%Y{}%m{}%d{} %X")
    return time_str.format("年", "月", "日")


def get_conn():
    # 建立连接
    conn = pymysql.connect(host="#", user="#", password="#", db="hotsearch", charset="utf8")
    # c创建游标A
    cursor = conn.cursor()
    return conn, cursor


def close_conn(conn, cursor):
    if cursor:
        cursor.close()
    if conn:
        conn.close()


def query(sql, *args):
    """

    :param sql:
    :param args:
    :return:
    """
    conn, cursor = get_conn()
    cursor.execute(sql, args)
    res = cursor.fetchall()
    close_conn(conn, cursor)
    return res


def get_baidu_data():
    sql = "select content,num from baiduhot order by id desc limit 30"
    res = query(sql)
    return res


def get_weibo_data():
    sql = "select content,num from weibohot order by id desc limit 50"
    res = query(sql)
    return res


def get_zhihu_data():
    sql = "select content,num from zhihuhot order by id desc limit 50"
    res = query(sql)
    return res

def get_bzhan_data():
    sql = "select content,num from bzhanhot order by id desc limit 100"
    res = query(sql)
    return res


if __name__ == "__main__":
    print(get_weibo_data())
    # print(test())
